package org.herrdommel.webpagechecker.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WPCSQLiteHelper extends SQLiteOpenHelper {

	public static final String TABLE_PAGES = "pages";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_PAGE = "page";
	public static final String COLUMN_RESULT = "result";
	public static final String COLUMN_CACHETIME = "cachetime";

	private static final String DATABASE_NAME = "wpc.db";
	private static final int DATABASE_VERSION = 2;

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table " + TABLE_PAGES + "(" + COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_PAGE + " text not null, " + COLUMN_RESULT
			+ " text not null, " + COLUMN_CACHETIME + " text not null);";

	public WPCSQLiteHelper(final Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(final SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
		Log.w(WPCSQLiteHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PAGES);
		onCreate(db);
	}

}
